<?php
namespace app\controller;

use app\model\Member as MemberModel;
use app\model\ViewHistory;
use think\facade\Session;
use think\facade\View;
use think\Request;
use app\BaseController;

class Member extends BaseController
{
    // 登录页面
    public function login()
    {
        return View::fetch('member/login');
    }
    
    // 登录处理
    public function doLogin(Request $request)
    {
        $username = $request->post('username');
        $password = $request->post('password');
        
        $member = MemberModel::where('username', $username)->find();
        
        if (!$member || !password_verify($password, $member->password)) {
            return json(['code' => 400, 'msg' => '用户名或密码错误']);
        }
        
        // 保存登录状态
        Session::set('member_id', $member->id);
        Session::set('member_name', $member->username);
        
        return json(['code' => 200, 'msg' => '登录成功']);
    }
    
    // 显示注册页面
    public function register()
    {
        return View::fetch('member/register');
    }
    
    // 处理注册请求
    public function doRegister(Request $request)
    {
        // 验证规则
        $validate = [
            'username' => 'require|length:4,20|alphaDash|unique:member',
            'email' => 'require|email|unique:member',
            'password' => 'require|length:6,32',
            'confirmPassword' => 'require|confirm:password',
            '__token__' => 'token'
        ];
        
        // 验证错误提示
        $validateMsg = [
            'username.require' => '用户名不能为空',
            'username.length' => '用户名必须为4-20位字符',
            'username.alphaDash' => '用户名只能包含字母、数字和下划线',
            'username.unique' => '用户名已被注册',
            'email.require' => '邮箱不能为空',
            'email.email' => '邮箱格式不正确',
            'email.unique' => '邮箱已被注册',
            'password.require' => '密码不能为空',
            'password.length' => '密码长度需在6-32位之间',
            'confirmPassword.require' => '请确认密码',
            'confirmPassword.confirm' => '两次密码输入不一致',
            '__token__.token' => '表单令牌验证失败'
        ];
        
        // 验证数据
        $result = $this->validate($request->post(), $validate, $validateMsg);
        
        if (true !== $result) {
            return json(['code' => 400, 'msg' => $result]);
        }
        
        try {
            // 创建会员
            $member = MemberModel::create([
                'username' => $request->post('username'),
                'email' => $request->post('email'),
                'password' => $request->post('password')
            ]);
            
            // 自动登录
            Session::set('member_id', $member->id);
            Session::set('member_name', $member->username);
            
            return json(['code' => 200, 'msg' => '注册成功']);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '注册失败: ' . $e->getMessage()]);
        }
    }
    
    // 退出登录
    public function logout()
    {
        Session::delete('member_id');
        Session::delete('member_name');
        return redirect('/player');
    }
    
    // 会员中心
    public function center()
    {
        if (!Session::has('member_id')) {
            return redirect('/member/login');
        }
        
        $member = MemberModel::with(['histories' => function($query) {
            $query->order('created_at', 'desc')->limit(20);
        }])->find(Session::get('member_id'));
        
        return View::fetch('member/center', [
            'member' => $member,
            'histories' => $member->histories
        ]);
    }
}